package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.dto.MerchantLevelRelationDTO;
import com.ayf.payment.game.api.dto.OrderDTO;
import com.ayf.payment.game.api.dto.criteria.MerchantLevelRelationCriteria;
import com.ayf.payment.game.api.entity.MerchantLevelRelation;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.MerchantService;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.ayf.payment.game.api.mapper.MerchantLevelRelationMapper;
import com.ayf.payment.game.api.service.MerchantLevelRelationService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class MerchantLevelRelationServiceImpl implements MerchantLevelRelationService{

    @Resource
    private MerchantLevelRelationMapper merchantLevelRelationMapper;

    @Autowired
    private MerchantService MerchantService;

    @Override
    @Transactional
    public void insert(MerchantLevelRelation entity,Integer level) {

        MerchantLevelRelation merchantLevelRelation=merchantLevelRelationMapper.selectByPrimaryKey(entity.getMerchantId());

        if(null ==merchantLevelRelation) {
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
            merchantLevelRelationMapper.insert(entity);
        }else{
            entity.setCreateTime(merchantLevelRelation.getCreateTime());
            entity.setUpdateTime(new Date());
            merchantLevelRelationMapper.updateByPrimaryKey(entity);
        }
        MerchantService.upateMerchantLevel(entity.getMerchantId(),level);
    }

    @Override
    public void update(MerchantLevelRelation entity) {
        merchantLevelRelationMapper.updateByPrimaryKey(entity);
    }

    @Override
    public MerchantLevelRelation selectOne(Integer merchantId) {
        return merchantLevelRelationMapper.selectByPrimaryKey(merchantId);
    }

    @Override
    public MerchantLevelRelationDTO getDTOByMerchantId(Integer merchantId) {

        return merchantLevelRelationMapper.getDTOByMerchantId(merchantId);
    }

    @Override
    public List<MerchantLevelRelationDTO> selectDTOBySupMerchantId(Integer supMerchantId) {

        return merchantLevelRelationMapper.selectDTOBySupMerchantId(supMerchantId);
    }

    @Override
    public List<MerchantLevelRelationDTO> selectPageByCriteria(MerchantLevelRelationCriteria criteria) {

        return merchantLevelRelationMapper.selectPageByCriteria(criteria);
    }
}
